/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.DAO;

import Model.Pojo.Group;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author luong
 */
public class GroupImp implements GroupDAO{
    
    Group group;

    @Override
    public boolean AjouterGroup(Group group) {
        boolean Resultat;
        return Resultat=Model.DAO.ModelDaoLibrarie.Ajouter(group);
    }

    @Override
    public boolean SuprimerGroup(Group group) {
        boolean Resultat=false;
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try {           
            String hql= String.format("select g from Group g where g.NameGroup='%s'",group.getNameGroup());
            group= (Group) session.createQuery(hql).uniqueResult();
            session.delete(group);
            session.beginTransaction().commit();
            Resultat=true;
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return Resultat;
    }

    @Override
    public boolean UpdateGroup(Group group) {
         boolean Resultat;
         return Resultat=Model.DAO.ModelDaoLibrarie.Update(group);
    }

    @Override
    public List<Group> GetListGroup() {
        List<Group> listGroup = null; 
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try {
            String hql="from Group";
            Query query= session.createQuery(hql);
            listGroup=query.list(); 
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return listGroup;
    }
    
}